package com.china08.yxyapi.api.manage.tec.teaching.scoreport.createexam;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.entity.mg.scoreport.Exam;
import com.china08.yxyapi.entity.mg.scoreport.Exam.Stu;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.scoreport.ExamRepository;

//@Api(tags = { "v53", "iiii" })
@CrossOrigin
@RestController
public class CreateExamUpdController {
    @Autowired
    private ExamRepository examRepository;

    @RequestMapping(value = "/api_yw/manage/tec/teaching/scoreport/createexam/upd2", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel)
     {
        RespModel model = new RespModel();
        Exam exam = examRepository.findById(reqModel.examId);
        ExamModel examTwo = new ExamModel();
        examTwo.name = exam.getName();// 考试名称
        examTwo.type = exam.getType();// 考试类型
        examTwo.subject = exam.getSubject();// 学科id
        examTwo.subjectName = exam.getSubjectName();// 学科名称
        examTwo.version = exam.getVersion();// 版本id
        examTwo.versionName = exam.getVersionName();// 版本name
        BookContent bookContent1 = null;
        List<BookContent> bookContentLst = new ArrayList<BookContent>();
        for (Exam.BookContent bookContent : exam.getBookContents()) {
            bookContent1 = new BookContent();
            bookContent1.id = bookContent.id;
            bookContent1.name = bookContent.name;
            bookContent1.pId = bookContent.pid;
            bookContentLst.add(bookContent1);
        }
        examTwo.bookContents = bookContentLst;// 章节
        examTwo.totalScore = exam.getTotalScore();// 考试总分
        Integer a= exam.getExamDate();
        StringBuilder sb=new StringBuilder(String.valueOf(a));
        sb.insert(4, "-");
        sb.insert(7, "-");
        examTwo.examDate = sb.toString();// 考试时间
        List<ExamQues> questions = new ArrayList<ExamQues>();
        Small small1 = null;
        List<Small> small2 = new ArrayList<Small>();
        List<ExamQues> questions3 = new ArrayList<ExamQues>();
        ExamQues examQues1 = null;
        Set<String> sectionNameSet = new HashSet<String>();
        if (exam.getQuestions() != null && exam.getQuestions().size() > 0) {
            for (Exam.ExamQues questions1 : exam.getQuestions()) {
                examQues1 = new ExamQues();
                examQues1.sectionName = questions1.sectionName;
                examQues1.score = questions1.score;
                if (questions1.noName != null && !questions1.noName.equals("")) {
                    sectionNameSet.add(examQues1.sectionName);
                    small1 = new Small();
                    small1.noName = questions1.noName;
                    small1.score = questions1.score;
                    small2.add(small1);
                } else {
                    // 包含没有小题的大题
                    questions.add(examQues1);
                }
                examQues1.small = small2;
                questions3.add(examQues1);
            }
            ;
        }

        // 包含有小题的大题
        List<Small> small = new ArrayList<Small>();
        ExamQues examQues = null;
        if (sectionNameSet.size() > 0) {
            for (String sectionNameSet1 : sectionNameSet) {
                for (ExamQues questions4 : questions3) {
                    if (sectionNameSet1.equals(questions4.sectionName)) {
                        examQues = new ExamQues();
                        examQues.sectionName = sectionNameSet1;
                        examQues.score = questions4.score;
                        small.addAll(questions4.small);
                        examQues.small = small;
                        questions.add(examQues);
                    }
                }
            }
        }

        examTwo.bigques = questions;// 试题
        examTwo.status = exam.getStatus();// 状态 0--未录入数据 , 1--未发送,2--已发送
        
        examTwo.semester = exam.getSemester();// 策别id
        examTwo.semesterName = exam.getSemesterName();// 策别name
        examTwo.stage = exam.getStage();//学段名称
        Student student = null;
        List<Student> studentLst = new ArrayList<Student>();
        for (Stu stu : exam.getStudents()) {
            student = new Student();
            student.gender = stu.gender;
            student.greadName = stu.grade_name;
            student.id = stu.student_id;
            student.name = stu.student_name;
            student.studentNo = stu.student_no;
            studentLst.add(student);
        }
        List<ClassModel> classId=new ArrayList<ClassModel>();
        ClassModel classModel = new ClassModel();
        classModel.classId = exam.getClassId();
        classModel.className = exam.getClassName();
        classModel.studentCount = studentLst;
        classId.add(classModel);
        examTwo.classId = classId;
        model.exam = examTwo;
        return model;
    }

    protected static class ReqModel extends YxyApiReqModel {
        public String examId;
    }

    protected static class RespModel extends YxyApiRespModel {
        // 考试实体
        public ExamModel exam;
    }

    protected class ExamModel {
        // 考试名称
        public String name;
        // 考试类型
        public String type;
        // 科目id
        public String subject;
        // 科目name
        public String subjectName;
        // 版本id
        public String version;
        // 版本name
        public String versionName;
        // 章节
        public List<BookContent> bookContents;
        // 考试总分
        public Integer totalScore;
        // 考试时间
        public String examDate;
        // 试题
        public List<ExamQues> bigques;
        // 状态 0--未录入数据 , 1--未发送,2--已发送
        public Integer status;
        // 班级id
        public List<ClassModel> classId;
        // 策别id
        public String semester;
        // 策别name
        public String semesterName;
        //学段
        public String stage;
    }

    public class ClassModel {
        // 班级id
        public String classId;
        // 班级name
        public String className;
        public List<Student> studentCount;

    }

    public class ExamQues {
        public String sectionName; // 大题名称
        public Integer score; // 分数
        public List<Small> small;// 小题集合
    }

    public class Small {
        public String noName;// 小题名称
        public Integer score;// 小题分数
    }

    public class BookContent {
        public String pId;
        public String id;
        public String name;
    }

    public class Student {
        // 学生id
        public String id;
        // 学生name
        public String name;
        // 学生学号
        public String studentNo;
        // 年级name
        public String greadName;
        // 性别
        public Integer gender;
    }
}
